package org.lee.cr;

/**
 * Account2
 *
 * @author dralee
 * @date 2022-01-13 10:52:20
 **/
public class Account3 {
    private int balance;
    private Object lock;

    public Account3(int balance, Object lock) {
        this.balance = balance;
        this.lock = lock;
    }

    public void transfer(Account3 target, int amount) {
        synchronized (lock) { // 锁住固定对象
            if (this.balance > amount) {
                this.balance -= amount;
                target.balance += amount;
            }
        }
    }

    @Override
    public String toString() {
        return "Account3{" +
                "balance=" + balance +
                '}';
    }
}
